package cn.muqingfeng.bbs.ui;

import cn.muqingfeng.bbs.R;
import cn.muqingfeng.bbs.constants.Constants;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WebViewActivity extends Activity implements OnClickListener {

	private WebView mWebView;
	private String mUrl;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.simple_webview);
		setupViews();
	}

	private void setupViews() {
		findViewById(R.id.back).setOnClickListener(this);
		mWebView = (WebView) findViewById(R.id.webview);
		WebSettings settings = mWebView.getSettings();
		settings.setJavaScriptEnabled(true);
		settings.setUseWideViewPort(true);
		settings.setBuiltInZoomControls(false);
		mWebView.setWebViewClient(new WebViewClient() {
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				if (!TextUtils.isEmpty(url) && (url.startsWith("http://") || url.startsWith("https://"))) {
					view.loadUrl(url);
				}
				return true;
			}
		});
		mUrl = getIntent().getStringExtra(Constants.EXTRA_URL);
		mWebView.loadUrl(mUrl);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;

		default:
			break;
		}
	}

	@Override
	public void onBackPressed() {
		if (mWebView.canGoBack()) {
			mWebView.goBack();
		} else {
			super.onBackPressed();
		}
	}

}
